package com.security.demo.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
public class UserAuthController {
	
	@RequestMapping(value = "/user/index", method = RequestMethod.GET)
	public ModelAndView userIndex() {
		return new ModelAndView("index");
	}
	
	@RequestMapping(value = "/admin/index", method = RequestMethod.GET)
	public ModelAndView indexAdmin() {
		return new ModelAndView("index_admin");
	}
	
	@RequestMapping(value = "/loginPage", method = RequestMethod.GET)
	public ModelAndView getLoginPage(boolean error) {
		ModelAndView mv = new ModelAndView("login");
		if (error) {
			mv.addObject("error",error);
		}
		return mv;
	}
	
	@RequestMapping(value = "/denied", method = RequestMethod.GET)
	public ModelAndView getDeniedPage() {
		return new ModelAndView("denied");
	}
	
	@RequestMapping(value = "/common", method = RequestMethod.GET)
	@PreAuthorize("hasAnyRole('USER','ADMIN')")
	public ModelAndView getCommonPage() {
		return new ModelAndView("common");
	}
	
	@RequestMapping(value = "/adminPage", method = RequestMethod.GET)
	public ModelAndView getAdminPage() {
		return new ModelAndView("page_admin");
	}
	
	@RequestMapping(value = "/userPage", method = RequestMethod.GET)
	public ModelAndView getUserPage() {
		return new ModelAndView("page_user");
	}
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView index() {
		return new ModelAndView("index");
	}

}